'use strict';


app.controller('RightsController', ['$cookieStore','infoAuthService' , '$scope', '$http', '$state','$window','MODE', function($cookieStore,infoAuthService, $scope, $http, $state, $window, MODE) {
	
	$scope.$on("page.query", function(event, data) {
		infoAuthService.getCount($scope.queryParam).then(function(data){
			if (data.code=='1001') {
				$scope.$broadcast("page.query.count",data);
			}
		});
		$scope.setRightsList($scope.roleId);
	});
	
	$scope.setRightsList = function(){
		var page = $cookieStore.get("page");
		infoAuthService.getAuthList(page, $scope.queryParam).then(function(data){
			if (data.code=='1001') {
					console.log("获取用户资料信息:", data );
					$scope.rightsList = data.data;
					
			} else if(data.code == '10010010'){  //非法请求
				$state.go("index");
			};
		});
	};
	
	$scope.formData = {};

	$scope.submit = function() {
		var auth = $scope.formData;
		if($scope.mode == MODE.add) {
			infoAuthService.addAuth(auth).then(function(data){
				if (data.code=='1001') {
					$scope.$emit("page.query");
					$scope.setRightsList($scope.page.pageNo);
				} else{
					alert("插入失败");
				};
			});
		} else if($scope.mode == MODE.edit) {
			infoAuthService.updateAuth(auth).then(function(data){
				if (data.code=='1001') {
					$scope.setRightsList($scope.page.pageNo);
				} else{
					alert("更新失败");
				};
			});
		}

		$('#rightsModal').modal('hide');
		$('#rightsModal').on('hidden.bs.modal', function () {
		  	$scope.close();
		})
	}
	
	$scope.query = function() {
		$scope.setRightsList($scope.page.pageNo);
	}
	
	$scope.close = function() {
		
		$scope.formData = {};
		$scope.form = {};
	}
	
	$scope.remove = function(id) {
		if($window.confirm("确定要删除该条信息?")) {
			infoAuthService.deleteById(id).then(function(data){
				if (data.code=='1001') {
					$scope.$emit("page.query");
					$scope.setRightsList($scope.page.pageNo);
				} else{
					alert('删除失败');
				};
				
			});
		}
	};
	
	$scope.setAddRights = function() {
		$scope.addRights = true;
		$scope.editRights = false;
		$scope.mode = MODE.add;
		$scope.close();
	}
	$scope.edit = function(id) {
		$scope.addRights = false;
		$scope.editRights = true;
		$scope.mode = MODE.edit;
		$scope.rightsList.forEach(function(rights){
			if (rights.id==id) {
				$scope.formData = rights;
			}
		})
		
	}
	
}]);